<?php

/*
 *  This file is part of MetaMVC.
 * 
 *  MetaMVC is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  MetaMVC is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with MetaMVC.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace MetaMVC\Framework\Routing;

/**
 * Description of Meta_Router
 *
 * @author artzter
 */
class RoutingTable {
    /**
     * Array of route objects.
     * @var array
     * @access private
     */
    private static $routes;
    
    /**
     * Adds a route to the routing table.
     * @param Meta_Route $route
     * @return void
     */
    public static function add($route) {
        self::$routes[] = $route;
    }
    
    /**
     * Tries to find a matching route from the routing table, and returns a request.
     * @param string $request
     * @return Meta_Request|null
     */
    public static function findMatch($source, $area, $defaults) {
        $match = null;
        
        if(count(self::$routes) > 0) {
            foreach (self::$routes as $route) {
                if($route->match(str_replace($area, '', $_SERVER['REQUEST_URI']))) {
                    $match = $route;
                    break;
                }
            }
            
            if($match != null) {
                //Generating a request
                $found["controller"] = $match->getController();
                $found["action"] = $match->getAction();
                
                
                // Getting Destination Controller
                if($found["controller"] != null) {
                    $destination["controller"] = $found["controller"];
                    unset($source[0]);
                } else {
                    //Controller is not defined, so we will look in the request.
                    if(!empty($source[0])) {
                        $destination["controller"] = $source[0];
                        unset($source[0]);
                    } else {
                        $destination["controller"] = $defaults["default_controller"];
                        unset($source[0]);
                    }
                }
                
                // Getting destination Action
                if($found["action"] != null) {
                    $destination["action"] = $found["action"];
                } else {
                    //Action is not defined, so we will look in the request.
                    if(isset($source[1]) && !empty($source[1])) {
                        $destination["action"] = $source[1];
                        unset($source[1]);
                    } else {
                        $destination["action"] = $defaults["default_action"];
                    }
                }
                
                if(count($source) > 0)
                    $args = $source;
                else
                    $args = array();
                
                return new Request($area, $destination["controller"], $destination["action"], $args);
            } else {
                throw new Exception("No suitable routes available.");
            }
        } else {
            throw new Exception("No routes are registered.");
        }
    }
}

?>
